import sqlite3
import bs4
import os
import pathlib
import time
conn = sqlite3.connect("data.db")
work_dir = pathlib.Path("tg_message")
with open(work_dir/"messages.html", "r", encoding="utf-8") as f:
    soup = bs4.BeautifulSoup(f.read(), "lxml")
for item in soup.select(".message.default.clearfix"):
    sticker = item.select_one(".sticker")
    media_file = item.select_one(".media")
    photo = item.select_one(".photo")
    upload_time = item.select_one(".pull_right.date.details").attrs["title"]
    time_array = time.strptime(upload_time, "%d.%m.%Y %H:%M:%S")
    timestamp = time.mktime(time_array)
    image = b""
    text = ""
    comment = ""
    if sticker:
        path = sticker.attrs["src"]
        os.system(
            f"ffmpeg -i \"{work_dir/path}\" \"{work_dir/(path+'.png')}\" -y")
        with open(work_dir/(path+".png"), "rb") as f:
            image = f.read()
    elif media_file:
        path = media_file.attrs["href"]
        with open(work_dir/path, "rb") as f:
            image = f.read()
    elif photo:
        path = photo.attrs["src"]
        with open(work_dir/path, "rb") as f:
            image = f.read()
    if item.select_one(".text"):
        text = "".join((str(x)
                        for x in item.select_one(".text").children)).strip()
    conn.execute("""INSERT INTO JIANGYAN (UPLOAD_TIME,IMAGE,TEXT,COMMENT) VALUES (?,?,?,?)""", [
                 timestamp, image, text, comment])
    # print(item)
    # print(image[:100], text)
    if not text and not image:
        print(item)
        print("-"*50)
conn.commit()
